package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.FormContentService;
import cn.kys.modules.system.service.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FormServiceImpl implements FormService {
    @Autowired
    private ToolsResposity toolsResposity;

    @Override
    public Map addForm(String name) {
        Map returnMap = new HashMap();
        String sql = "SELECT * FROM public.t_form where name = '" + name + "'";
        Map formMap = toolsResposity.exeSql(sql);
        if(formMap==null){
            String sql_insert = "INSERT INTO public.t_form(name) VALUES ('" + name + "');";
            toolsResposity.exeSql(sql_insert);
            returnMap.put("success",true);
        }else {
            returnMap.put("success",false);
            returnMap.put("msg","表单项目已经存在");
        }

        return returnMap;
    }

    @Override
    public Map editForm(String id, String name,String formcontentids) {
        String sql = "UPDATE t_form SET name = '" + name + "' WHERE id ='" + id + "'";
        toolsResposity.exeSql(sql);

        //更新t_form_content表,先删除，再插入

        //删除
        String sql_delete = "delete from t_form_content where formid ="+id;
        toolsResposity.exeSql(sql_delete);
        //更新
        if(!"".equals(formcontentids)){
            String formcontentidArr[] = formcontentids.split(",");
            for(int i=0;i<formcontentidArr.length;i++){
                String formcontentid = formcontentidArr[i];
                String sql_formcontent = "select * from t_formcontent where id = "+formcontentid;
                Map formcontentMap = toolsResposity.exeSql(sql_formcontent);
                if(formcontentMap!=null){
                    String formcontentname = formcontentMap.get("name").toString();
                    String formcontentcname = formcontentMap.get("cname").toString();
                    String type = formcontentMap.get("type").toString();
                    int ordernum = i+1;
                    String sql_insert = "INSERT INTO public.t_form_content(formid,formcontentid,formname,formcontentname,formcontentcname,type,is_show,ordernum) VALUES (" + id +",'"+ formcontentid +"','"+ name +"','"+ formcontentname +"','"+formcontentcname+"','"+type+"',"+false+','+ordernum+");";
                    toolsResposity.exeSql(sql_insert);
                }

            }
        }
        return new HashMap();

    }

    @Override
    public void updateFormContentShowStatus(String ids) {
        //更新
        if(!"".equals(ids)){
            String idArr[] = ids.split(",");
            for(int i=0;i<idArr.length;i++){
                String id = idArr[i];
                String sql = "UPDATE t_form_content SET is_show = " + true + " WHERE id ='" + id + "'";
                toolsResposity.exeSql(sql);
            }
        }
    }

    @Override
    public List getAllForms() {
        String sql = "SELECT * FROM public.t_form";
        return toolsResposity.exeSqlList(sql);
    }

    /*@Override
    public Map delFormById(String id) {
        Map returnMap = new HashMap();

        String sql = "select * from t_form_content where formid in (" + id + ")";
        List formContentMap = toolsResposity.exeSqlList(sql);
        if (formContentMap.size() > 0) {
            //不能删
            returnMap.put("deleted", false);
            returnMap.put("msg", "表单表中使用了该图层，不能删除");
        } else {
            //可以删除
            String sql_delete = "delete from t_form where id in (" + id + ")";
            toolsResposity.exeSql(sql_delete);

            String sql_form_content_delete = "delete from t_form_content where formid in (" + id + ")";
            toolsResposity.exeSql(sql_form_content_delete);

            returnMap.put("deleted", true);
        }

        return returnMap;
    }*/

    @Override
    public Map delFormById(String id) {
        Map returnMap = new HashMap();


        //可以删除
        String sql_delete = "delete from t_form where id in (" + id + ")";
        toolsResposity.exeSql(sql_delete);

        String sql_form_content_delete = "delete from t_form_content where formid in (" + id + ")";
        toolsResposity.exeSql(sql_form_content_delete);

        returnMap.put("deleted", true);


        return returnMap;
    }

    @Override
    public List getBindingByFormId(String id) {
        String sql = "SELECT * FROM public.t_form_content where formid = "+id+" order by ordernum";
        return toolsResposity.exeSqlList(sql);
    }
}

